import { FMVCS } from "../../fgui/fmvc/FMVCS";
import Star from "../component/star/Star";
import EventSystem from "../../core/EventSystem";
import { GlobalEvent } from "../global/Global";
import { ConstDefine } from "../common/ConstDefine";
import PlayerCtrl from "../player/PlayerCtrl";
import MapCreateCtrl from "./MapCreateCtrl";

/** 翻翻乐地图编辑器 */
export default class MapCreateView extends FMVCS.FView {
    /** 地图星星 */
    private _starList: Star[] = [];
    /** 星星块 */
    private _chooseStarList: Star[] = [];
    /** 上一次选择的星星 */
    private _preSelStar: Star = null;
    /** 当前选择的星星 */
    private _curSelStar: Star = null;
    /** 是否正在触摸屏幕状态 */
    private _isTouch: boolean = false;
    /** 是否有编辑过地图星星 */
    private _isMapChange: boolean = false;
    clickHandlers(){
        return [
            [this._views[`btnBack`], this.onBtnBack, this],
            [this._views[`btnClean`], this.onBtnClean, this],
            [this._views[`btnRelease`], this.onBtnRelease, this],
        ];
    }

    onOpenCallBack(){
        this._isMapChange = false;
        this.initMap();
        this.initChooseStar();
        this.onFlush();
    }

    initMap(){
        let starContent = this._views[`starMapContent`];
        starContent.on(fgui.Event.TOUCH_BEGIN, () => {
            this._isTouch = true;
        });

        starContent.on(fgui.Event.TOUCH_END, () => {
            this._isTouch = false;
        });

        let fillStar = (star: Star) => {
            if(this._curSelStar){
                this._isMapChange = true;
                star.setStarTypeImg(this._curSelStar.type);
            }
        }

        for(let i = 0; i < 175; ++i){
            let star = Star.createStar();
            starContent.addChild(star);
            star.setPosByIndex(i);
            star.setStarTypeImg(-1);
            this._starList[i] = star;
            star.index = i;
            star.on(fgui.Event.ROLL_OVER, () => {
                if(this._isTouch){
                    fillStar(star);
                }
            });
            star.onClick(() => {
                fillStar(star);
            });
        }
        if(MapCreateCtrl.data.vo.revisionMap){
            let map = MapCreateCtrl.data.vo.revisionMap.map;
            for(let i = 0; i < map.length; ++i){
                let starType = map[i][0];
                let index = map[i][1];
                this._starList[index].setStarTypeImg(starType);
            }
        }
    }

    initChooseStar(){
        let list = this._views[`chooseStarList`].asList;
        fgui.UIObjectFactory.setExtension("ui://Common/btnStar", Star);
        for(let i = -1; i < 7; ++i){
            let star = list.addItemFromPool() as Star;
            star.setStarTypeImg(i);
            star.index = i;
            this._chooseStarList[i] = star;
            star.onClick(() => {
                if(this._curSelStar != star){
                    this._preSelStar = this._curSelStar;
                    this._curSelStar = star;

                    if(this._preSelStar)
                        this._preSelStar.setScale(1, 1);
                    if(this._curSelStar)
                        this._curSelStar.setScale(1.5, 1.5);
                }
            });
        }
    }

    onBtnBack(){
        EventSystem.emit(GlobalEvent.UIListenID.UI_MapLocalView_Open);
        this.onClose();
    }

    onBtnClean(){
        for(let i = 0; i < this._starList.length; ++i){
            let star = this._starList[i];
            star.setStarTypeImg(-1);
        }
    }

    onBtnRelease(){
        let map = [];
        for(let i = 0; i < this._starList.length; ++i){
            let star = this._starList[i];
            if(star.type >= 0){
                map.push([star.type, star.index]);
            }
        }
        let types: {[type: number]: boolean} = {};
        for(let i = 0; i < map.length; ++i){
            let type = map[i][0];
            types[type] = true;
        }
        let answerStar = [];
        for(let type in types){
            answerStar.push(Number(type));
        }
        let mapId = this._views[`lblInput`].asLabel.text;
        if(!MapCreateCtrl.data.vo.isNewMap){
            mapId = MapCreateCtrl.data.vo.revisionMap.id;
        }
        if(map.length > 0){
            if(answerStar.length > 1){
                if(this._isMapChange){
                    let starConfig: ConstDefine.Config.StarConfig = {
                        id: mapId,
                        map: map,
                        step: 0,
                        answerStar: answerStar,
                        mapName: this._views[`lblInput`].asLabel.text
                    }
                    this.onClose();
                    if(MapCreateCtrl.data.vo.isNewMap)
                        EventSystem.emit(GlobalEvent.GameEventID.GameMapCreate, starConfig);
                    else
                        EventSystem.emit(GlobalEvent.GameEventID.GameMapRivisions, starConfig);
                }else{
                    EventSystem.emit(GlobalEvent.UIListenID.UI_ShowTips, `地图没有被修改过!`);
                }
            }else{
                EventSystem.emit(GlobalEvent.UIListenID.UI_ShowTips, `地图上星星种类必须大于1!`);
            }
        }else{
            EventSystem.emit(GlobalEvent.UIListenID.UI_ShowTips, `地图上没有星星!`);
        }
    }

    onFlush(type: string = `all`){
        switch(type){
            case `all`: {
                this._updateLblInput();
                break;
            }
        }
    }

    private _updateLblInput(){
        this._views[`lblInput`].asLabel.text = PlayerCtrl.data.vo.nickName + `的房间`;
    }
}